/**************************************************************************************
*  Buzz.java
*  Program demonstrating the use of PWM with LPC12xx.
**************************************************************************************
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Created by:
*      RTJ Computing
**************************************************************************************/
import lpc2xxx.comm.*;
import lpc2xxx.hw.*;
import lpc2xxx.edu.*;


class Buzz
{
    // duty is controlled in 0.01% increments
    final static int on = 0;
    final static int off = 10000;
    static Switch sw = new Switch();
    
    static void main(String[] args) 
    {
        Console.println("");
        Console.println("LPC21xx PWM Buzzer demo");
        Console.println("This program is for use with Education board with buzzer on PWM2");
        Console.println("Jumper settings:");
        Console.println("  connect    J16\r\n");
        Console.println("Use joystick up/down buttons to change volume.");

        sw.start();
        
        int duty = off;
        String in;
        
        PWM.init(PWM.PWM2, 1000);
        PWM.duty(PWM.PWM2, off);
        PWM.apply();

        showMenu();
        while (true)
        {
            char key = Console.getKey();
            if (key == Console.ESC)
                break;

            int newDuty = -1;
            switch (key)
            {
                case '1':
                    newDuty = on;
                    break;
                case '2':
                    newDuty = off;
                    break;
                case '3':
                    Console.print("\r\nEnter volume (0-100): ");
                    in = Console.getKeys();
                    Console.println("");
                    if (in != null)
                    {
                        try 
                        {
                            int v = Integer.parseInt(in);
                            if (v >= 0 && v <= 100)
                                newDuty = (100 - v) * 100;
                            else
                                Console.println("Invalid value entered");

                        }
                        catch (NumberFormatException e)
                        {
                            Console.println("Invalid characters in the input");
                        }
                    }
                    showMenu();
                    break;
            }

            if (sw.up() && duty > 0)
                newDuty = duty - 500;
            else if (sw.down() && duty < 10000)
                newDuty = duty + 500;
            if (newDuty != -1)
            {
                if (newDuty < 0)
                    duty = 0;
                else if (newDuty > 10000)
                    duty = 10000;
                else
                    duty = newDuty;

                PWM.duty(PWM.PWM2, duty);
                PWM.apply();
                
                int perc = 100 - (duty / 100);
                Console.print("\r" + Integer.toString(perc) + "%  ");
                
                try { Thread.sleep(200); } catch (InterruptedException e) {}
            }
        }
        // turn buzzer off
        PWM.duty(PWM.PWM2, off);
        PWM.apply();
    }

    static void showMenu()
    {
        Console.println("");
        Console.println("Press:");
        Console.println("   1   - turn buzzer on");
        Console.println("   2   - turn buzzer off");
        Console.println("   3   - set volume (or use joystick up/down buttons)");
        Console.println("   ESC - exit program");
    }
}

